package gw.com.android.ui.views;

import android.content.Context;
import android.content.res.TypedArray;
import android.text.TextWatcher;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.gwtsz.gts2.hx.R;

import www.com.library.view.NumberInput;
import www.com.library.view.TintImageView;

/**
 * 各类价格数字调整控件，带左右按钮
 * @author Administrator
 *
 */
public class NumericEdit extends RelativeLayout {

	public static final int BTN_ID_FAST_DECREMENT = 0; //大浮动减少
	public static final int BTN_ID_FAST_INCREMENT = 1;//大浮动增加
	public static final int BTN_ID_FINE_DECREMENT = 2;//小浮动减少
	public static final int BTN_ID_FINE_INCREMENT = 3;//小浮动增加
	public static final int BTN_ID_UNKNOWN = -1;

	public static final int TYPE_FINE_AND_FAST_STEPS = 0; //每次浮动范围大，默认为5个点
	public static final int TYPE_FINE_STEP_ONLY = 1;//每次浮动范围少，默认为0.01个点

	private int mType = TYPE_FINE_AND_FAST_STEPS; //浮动类型 ，大范围还是小范围
	private boolean mRepeatBtnFired = true;
	private TintImageView mBtnFastDec = null;
	private TintImageView mBtnFineDec = null;
	private TintImageView mBtnFastInc = null;
	private TintImageView mBtnFineInc = null;
	private NumberInput mInputEdit = null; //价格输入框
	private TextView mInputPoint = null;//点
	private final OnClickListener mBtnClickListenerInternal = new OnClickListener() {
		@Override
		public void onClick(View v) {
			if (v == mBtnFastInc) {
				mInputEdit.incMultipleSteps();
			} else if (v == mBtnFastDec) {
				mInputEdit.decMultipleSteps();
			} else if (v == mBtnFineInc) {
				mInputEdit.incMinSteps();
			} else if (v == mBtnFineDec) {
				mInputEdit.decMinSteps();
			} else {
			}
			mInputEdit.requestFocus();
			mInputEdit.setFocusable(true);
			mInputEdit.setSelection(mInputEdit.getText().length());
		}
	};
	
	public NumericEdit(Context context, AttributeSet attrs,int defStyle) {
		super(context, attrs, defStyle);

		/* Obtain private attribute set */
		obtainAttributeSet(context, attrs);

		/* Initialize the internal parameters */
		init(context);
	}

	public NumericEdit(Context context, AttributeSet attrs) {
		super(context, attrs);

		/* Obtain private attribute set */
		obtainAttributeSet(context, attrs);

		/* Initialize the internal parameters */
		init(context);
	}

	public NumericEdit(Context context) {
		super(context);

		/* Initialize the internal parameters */
		init(context);
	}
	
	private void obtainAttributeSet(Context c, AttributeSet attrs) {
		TypedArray a = c.obtainStyledAttributes(attrs, R.styleable.AdjustableNumericEdit);

		final int N = a.getIndexCount();
		for (int i = 0; i < N; i++) {
			int attr = a.getIndex(i);
			switch (attr) {
			case R.styleable.AdjustableNumericEdit_adjusttype:
				mType = a.getInt(attr, TYPE_FINE_AND_FAST_STEPS);
				break;
			default:
				break;
			}
		}

		a.recycle();
	}

	
	private void init(Context ctx) {
		setGravity(Gravity.CENTER);
		LayoutInflater inflater = LayoutInflater.from(ctx);
		View view = inflater.inflate(R.layout.order_numeric_edit, this, true);

		mBtnFastDec = (TintImageView) view.findViewById(R.id.input_left_fast_btn);
		mBtnFineDec = (TintImageView) view.findViewById(R.id.input_left_btn);
		mInputEdit = (NumberInput) view.findViewById(R.id.number_input);
		mBtnFineInc = (TintImageView) view.findViewById(R.id.input_right_fast_btn);
		mBtnFastInc = (TintImageView) view.findViewById(R.id.input_right_btn);
		mInputPoint = (TextView) view.findViewById(R.id.input_point);
		mInputPoint.setVisibility(View.GONE);
		
		mBtnFastDec.setOnClickListener(mBtnClickListenerInternal);
		mBtnFineDec.setOnClickListener(mBtnClickListenerInternal);
		mBtnFineInc.setOnClickListener(mBtnClickListenerInternal);
		mBtnFastInc.setOnClickListener(mBtnClickListenerInternal);

		if (mRepeatBtnFired) {
			mBtnFastDec.setOnTouchListener(new OnTouchHoldListener(mBtnClickListenerInternal));
			mBtnFineDec.setOnTouchListener(new OnTouchHoldListener(mBtnClickListenerInternal));
			mBtnFineInc.setOnTouchListener(new OnTouchHoldListener(mBtnClickListenerInternal));
			mBtnFastInc.setOnTouchListener(new OnTouchHoldListener(mBtnClickListenerInternal));
		}

		/* Configure the adjustableNumericInput type */
		setAdjustableType(mType);
	}

	public void setButtonRepeatFired(boolean repeated) {
		mRepeatBtnFired = repeated;
	}

	public View getFastIncButton() {
		return mBtnFastInc;
	}

	public View getFastDecButton() {
		return mBtnFastDec;
	}

	public View getFineIncButton() {
		return mBtnFineInc;
	}

	public View getFineDecButton() {
		return mBtnFineDec;
	}

	public NumberInput getNumericInput() {
		return mInputEdit;
	}

	public void setText(String text) {
		mInputEdit.setTextValue(text);
	}
	
	public void setPointVisible(int visible){
		mInputPoint.setVisibility(visible);
	}
	/**
	 * 根据小数位返回是否为0
	 * @param digit
	 * @return
	 */
	public boolean isEmptyOrZero(int digit){
		return mInputEdit.isEmptyOrZero(digit);
	}

	/**
	 * 止损止盈选项，判断输入框是否输入空值
	 * @return
	 */
	public boolean isEmpty(){
		String value = getText().toString();
		if(value.equals("")){
			return true;
		}else{
			return false;
		}
	}
	
	/**
	 * 设置后缀值
	 * @param str
	 */
	public void setSuffixStr(String str){
		mInputEdit.setSuffixStr(str);
	}
	
	/**
	 * 设置默认值
	 * @param value
	 */
	public void setDefalutValue(String value){
		mInputEdit.setDefalutValue(value);
	}
	
	/**返回数值*/
	public double getValue(){
		return mInputEdit.getValue();
	}
	
	/**返回数值*/
	public float getFloatValue(){
		return mInputEdit.getFloatValue();
	}
	
	/**返回数值*/
	public int getIntValue(){
		return mInputEdit.getIntValue();
	}
	
	/**返回输入框值*/
	public String getValueString() {
		return mInputEdit.getValueString();
	}

	public String getText() {
		return mInputEdit.getStrValue();
	}

	public void disabledWithText(String text) {
		setEnabled(false);
		mInputEdit.disabledWithText(text);
	}
	
	/**
	 * 设置输入框内输入数字的范围
	 * @param beforeDot
	 * @param afterDot
	 */
	public void setDigits(int maxlength,int beforeDot, int afterDot) {
		mInputEdit.setDigits(maxlength, beforeDot, afterDot);
	}

	public void setDigits(String vaule) {
		if(vaule.contains(".")){
			setDigits(vaule.length(), vaule.indexOf("."), vaule.length() - vaule.indexOf(".") - 1);
		}else{
			setDigits(vaule.length(), vaule.length(), 0);
		}
	}

	public void setDecimalDigits(int digits) {
		mInputEdit.setDecimalDigits(digits);
	}

	public void addTextChangedListener(TextWatcher watcher) {
		mInputEdit.addTextChangedListener(watcher);
	}

	public void setAfterTextChangedListener(NumberInput.OnTextChangedListener l) {
		mInputEdit.setAfterTextChangedListener(l);
	}

	public void setMin(double min) {
		mInputEdit.setMin(min);
	}

	public void setMax(double max) {
		mInputEdit.setMax(max);
	}

	public void setMultipleSteps(int multiple) {
		mInputEdit.setMultipleSteps(multiple);
	}

	public void setMinSteps(double step) {
		mInputEdit.setMinSteps(step);
	}

	public void setLocked(boolean locked) {
		final boolean enableBtn = !locked;

		setAdjustableType(mType);
		mBtnFastDec.setEnabled(enableBtn);
		mBtnFineDec.setEnabled(enableBtn);
		mInputEdit.setEnabled(enableBtn, false);
		mBtnFineInc.setEnabled(enableBtn);
		mBtnFastInc.setEnabled(enableBtn);
	}

	@Override
	public void setEnabled(boolean enabled) {
		super.setEnabled(enabled);

		if (enabled) {
			setAdjustableType(mType);
		} else {
			final int viewState = enabled ? View.VISIBLE : View.GONE;
			mBtnFastDec.setVisibility(viewState);
			mBtnFineDec.setVisibility(viewState);
			mInputEdit.setEnabled(enabled);
			mBtnFineInc.setVisibility(viewState);
			mBtnFastInc.setVisibility(viewState);
		}
	}

	/**
	 * 设置浮动范围类型，可选类型为两种
	 * @param type
	 */
	public void setAdjustableType(int type) {
		mType = type;
		if (mType == TYPE_FINE_AND_FAST_STEPS) {
			mBtnFastInc.setVisibility(View.VISIBLE);
			mBtnFastDec.setVisibility(View.VISIBLE);
			mBtnFineInc.setImageResource(R.mipmap.a_smalledit_increase);
			mBtnFineDec.setImageResource(R.mipmap.a_smalledit_reduce);
		} else {
			mBtnFastInc.setVisibility(View.GONE);
			mBtnFastDec.setVisibility(View.GONE);
			mBtnFineInc.setImageResource(R.mipmap.a_smalledit_increase);
			mBtnFineDec.setImageResource(R.mipmap.a_smalledit_reduce);
		}
	}
}
